package cn.zyx.order.controller;

import cn.zyx.basic.util.AjaxResult;
import cn.zyx.basic.util.LoginContext;
import cn.zyx.basic.util.PageList;
import cn.zyx.order.domain.AdoptOrder;
import cn.zyx.order.query.AdoptOrderQuery;
import cn.zyx.order.service.IAdoptOrderService;
import cn.zyx.user.domain.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 领养订单接口
 */
@RestController
@RequestMapping("/adopt")
public class AdoptOrderController {

    @Autowired
    private IAdoptOrderService adoptOrderService;

    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody Map<String,Object> params, HttpServletRequest request){
        try{
            String payData = adoptOrderService.submit(params, LoginContext.getLoginInfo(request));
            System.out.println(payData);
            return AjaxResult.me().setResultObj(payData);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("下单失败！"+e.getMessage());
        }

    }

    /**
     * 领养订单后台查询接口
     *   平台管理
     *   店铺员工（店铺管理员  + 员工）
     * @param query
     * @return
     */
    @PostMapping("/admin")
    public PageList<AdoptOrder> queryAdmin(@RequestBody AdoptOrderQuery query, HttpServletRequest request){
        try {
            LoginInfo loginInfo = LoginContext.getLoginInfo(request);
            return adoptOrderService.queryAdmin(query, loginInfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 前端用户查询自己领养订单
     * @param query
     * @return
     */
    @PostMapping("/user")
    public PageList<AdoptOrder> queryUser(@RequestBody AdoptOrderQuery query, HttpServletRequest request){
        try {
            LoginInfo loginInfo = LoginContext.getLoginInfo(request);
            return adoptOrderService.queryUser(query, loginInfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
